﻿@model IntroMVC4.Models.Domain.ProductSKU
@{
    ViewBag.Title = "SuaSanPham";
    Layout = "~/View/View/Shared/_sbAdmin.cshtml";
}
@*<link href="/Content/bootstrap/bootstrap.css" rel="stylesheet"/>*@
@using (Html.BeginForm("SuaSanPham", "Admin"))
{    
    
    <div class="panel panel-default">
        <div class="panel-heading">
            <h5>Thông Tin Sản Phẩm
                <button class="pull-right btn btn-small btn-primary" type="submit">Lưu sản phẩm</button></h5>
        </div>
        <div class="panel-body">
            <div class="input-group col-md-8">
                <span class="input-group-addon">Mã sản phẩm</span>
                <input class="form-control" type="text" maxlength="50" value="@Model.SKU" id="SKU" name="SKU" />
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <span class="input-group-addon">Mã quốc tế</span>
                <input class="form-control" type="text" maxlength="50" value="@Model.GTIN" id="GTIN" name="GTIN" />
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <span class="input-group-addon">Tên sản phẩm</span>
                <input class="form-control" type="text" maxlength="50" value="@Model.Name" id="Name" name="Name" />
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <span class="input-group-addon">Số lượng hiện có</span>
                <input class="form-control" type="text" value="@Model.Quantity" id="Quantity" name="Quantity" disabled/>
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <span class="input-group-addon">Hãng</span>
                @Html.DropDownList("PrincipalCategoryId", ViewData["PrincipalCategories"] as List<SelectListItem>, String.Empty, new { @class = "form-control" })
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <span class="input-group-addon">Loại Sản Phẩm</span>
                <input type="hidden" value="@Model.ProductCatalogId" name="ProCatalogId" id="ProCatalogId" />
                @Html.DropDownList("ProductCatalogId", ViewData["ProductCategories"] as List<SelectListItem>, String.Empty, new { @disabled = "disabled", @class = "form-control" })
            </div>
            <div class="clearfix"></div>
            <br />
            <div class="input-group col-sm-8">
                <label>
                    <input type="checkbox" checked="@Model.IsActive" id="IsActive" name="IsActive">
                    Hiển thị sản phẩm này
                </label>
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h5>Thông Tin Thuộc Tính
                <button class="pull-right btn btn-small btn-primary" type="button" data-bind="click: addAttributePro">Thêm thuộc tính</button></h5>
        </div>
        <div class="panel-body">
            @*@foreach (var item in Model.ListAttributeProductD)
            {
                <input type="hidden" id="product" name="product" value="@item.ProductId"/>
                foreach (var item1 in item.ListAttributeProduct)
                {
                <div class="input-group col-sm-2">
                    <span class="input-group-addon">@item1.Name</span>
                    <input class="form-control" type="text" value="@item1.Value" id="@item1.Name" name="@item1.Name" />
                </div>
                }                
                <div class="clearfix"></div>
                <br />
            }*@

            <div data-bind='foreach: viewModelJS'>
                <input type="hidden" id="productId" name="productId" data-bind="value:ProductId" />
                <div data-bind='foreach: ListAttributeProduct'>
                    <div class="input-group col-sm-2">
                        <span class="input-group-addon" data-bind="text:Name"></span>
                        <input class="form-control" type="text" maxlength="10" data-bind="attr: {id:Name, name: Name,value: Value}" />
                    </div>
                </div>
                <button class="pull-right btn btn-small btn-primary" type="button" data-bind="click: $root.remoteAttributePro">Xóa thuộc tính</button>
                <div class="clearfix"></div>
                <br />
            </div>
        </div>

    </div>
}
<div>
    @Html.ActionLink("Back to List", "SanPham")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
<script type="text/javascript">
    function addAttributePro()
    {        
        var modelJS = @Html.Raw(Json.Encode(Model.ListAttributeProductD));
        
        alert(model);
    }
    var AttributeProModel = function(viewModelJS) {
        var self = this;
        self.viewModelJS = ko.observableArray([]);
        var subList= '{"ProductId":0,"ListAttributeProduct":[';
            
        for(var i=0;i<viewModelJS.length;i++){
            var obj = viewModelJS[i];
            for(var ii=0;ii<obj["ListAttributeProduct"].length;ii++){
                if(ii>0)
                {
                    subList = subList + ",";
                }
                subList = subList + '{"ProductId":0,"Name":"'+obj["ListAttributeProduct"][ii].Name+'","Value":"","StartDate":null,"EndDate":null}';
                //alert(subList);
            }
            break;
        }            
        subList = JSON.stringify(eval('('+subList+']})'));
        self.addAttributePro = function() {
            self.viewModelJS.push(JSON.parse(subList));
        };
            
        self.remoteAttributePro = function(AttributePro) {
            self.viewModelJS.remove(AttributePro);
        };
 
        //self.save = function(form) {
        //    alert("Could now transmit to server: " + ko.utils.stringifyJson(self.gifts));
        //    // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.gifts);
        //};
        self.viewModelJS(viewModelJS);
    };
 
    var viewModel = new AttributeProModel(@Html.Raw(Json.Encode(Model.ListAttributeProductD)));
        ko.applyBindings(viewModel);
</script>
